package com.warmlight.voicepacket;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.text.style.URLSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.warmlight.voicepacket.base.BaseActivity;
import com.warmlight.voicepacket.utils.PermissionPageUtils;
import com.warmlight.voicepacket.utils.ToastUtil;

/**
 * Created by jason on 2018/6/28.
 */

public class FloatWindowPermissionActivity extends BaseActivity {

    private TextView tv_go2set,tv_iknow,tv_float_hint;
    private ImageView iv_p_grey;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_float_permission);

        initView();
    }

    private void initView(){
        tv_iknow = (TextView) findViewById(R.id.tv_iknow);
        tv_go2set = (TextView) findViewById(R.id.tv_go2set);
        tv_float_hint = (TextView) findViewById(R.id.tv_float_hint);
        iv_p_grey = (ImageView) findViewById(R.id.iv_p_grey);

        String hint = "使用语音包还需要开启悬浮窗，请点击下方“设置”按钮，在弹出的系统权限界面中找到“悬浮窗”权限并开启，若遇到问题，请进入悬浮窗攻略";
        SpannableString ss = new SpannableString(hint);

        ss.setSpan(new URLSpan("http://m.nuanguang.vip/wanpi/Suspension.html"), hint.lastIndexOf("悬浮窗攻略"), hint.lastIndexOf("攻略")+2,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new ForegroundColorSpan(Color.RED),hint.lastIndexOf("悬浮窗攻略"), hint.lastIndexOf("攻略")+2,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            //SpannableString对象设置给TextView
        tv_float_hint.setText(ss);
            //设置TextView可点击
        tv_float_hint.setMovementMethod(LinkMovementMethod.getInstance());


        tv_iknow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        tv_go2set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PermissionPageUtils permissionPageUtils = new PermissionPageUtils(getThis());
                permissionPageUtils.jumpPermissionPage();

//                Intent intent = new Intent();
////                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
////                        Uri.parse("package:" + getPackageName()));
//                intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
//                intent.setData(Uri.fromParts("package", getPackageName(), null));
////                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
////                        Uri.parse("package:" + getPackageName()));
////                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
////                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
////                        Uri.parse("package:" + getPackageName()));
//                getThis().startActivityForResult(intent,100);
//                finish();
            }
        });

//        cb_permission.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//                SPUtils.setFloatWindowNotice(!cb_permission.isChecked());
//            }
//        });

        iv_p_grey.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        findViewById(R.id.ll_interupt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Do nothing
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (Settings.canDrawOverlays(this)) {
                    createWindow();
//                    SPUtils.setFloatWindowNotice(false);
                    finish();
                }else{
                    ToastUtil.showShort(this, "悬浮窗权限已被拒绝");
                    finish();
                }
            } else {
                //Toast.makeText(this, "权限已被拒绝", Toast.LENGTH_SHORT).show();
                finish();
            }
        }

    }

    private void createWindow(){
        startService(new Intent(getThis(),FloatWindowService.class));
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (Settings.canDrawOverlays(this)) {
                startService(new Intent(getThis(),FloatWindowService.class));
                finish();
                return;
            }else{

            }
        }else {
            startService(new Intent(getThis(),FloatWindowService.class));
        }
        finish();
    }



}
